﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simulate_control
{
    #region USING
    using simulate_control.Helpers;
    using simulate_control.Basic;
    using simulate_control.Repositories;
    using simulate_control.Models;
    #endregion

    public partial class FrmMain : Form
    {
        #region VARIABLES
        private int numSecond;
        private TURNMODE mode;
        private VIEWMODE viewMode = VIEWMODE.CLEAR;
        private KEY KeyEvent;
        private DISPMODE DispMode = DISPMODE.NONE;
        private FrmDimmerAndContrast frmDimmerAndContrast;
        private FrmMainMenu frmMainMenu;
        private FrmPlotterSetup frmPlotterSetup;
        private FrmSystemSetup frmSysSetup;
        private FrmShiftingCursor frmShiftingCursor;
        private FrmWayPoint frmWayPoint;
        private FrmSelectWayPointList frmSelectWayPointList;
        private FrmWaypointList frmWayPointList;
        #endregion

        public FrmMain()
        {
            InitializeComponent();
        }

        public void InitForms()
        {
            // Init Dimmer and contrast form
            frmDimmerAndContrast = new FrmDimmerAndContrast();
            frmDimmerAndContrast.TopLevel = false;
            frmDimmerAndContrast.FormBorderStyle = FormBorderStyle.None;
            frmDimmerAndContrast.Dock = DockStyle.Fill;
            frmDimmerAndContrast.Handler += HandlerAdjustDimmerAndConstrast;

            // Init Main menu form
            frmMainMenu = new FrmMainMenu();
            frmMainMenu.TopLevel = false;
            frmMainMenu.FormBorderStyle = FormBorderStyle.None;
            frmMainMenu.Dock = DockStyle.Fill;
            frmMainMenu.Handler += HandlerMainMenu;

            // Init Plotter setup form
            frmPlotterSetup = new FrmPlotterSetup();
            frmPlotterSetup.TopLevel = false;
            frmPlotterSetup.FormBorderStyle = FormBorderStyle.None;
            frmPlotterSetup.Dock = DockStyle.Fill;
            //frmPlotterSetup.Handler += HandlerMainMenu;

            // SYSTEM SETUP
            frmSysSetup = new FrmSystemSetup();
            frmSysSetup.TopLevel = false;
            frmSysSetup.FormBorderStyle = FormBorderStyle.None;
            frmSysSetup.Dock = DockStyle.Fill;

            // SHIFTING CURSOR
            frmShiftingCursor = new FrmShiftingCursor();
            frmShiftingCursor.TopLevel = false;
            frmShiftingCursor.FormBorderStyle = FormBorderStyle.None;
            frmShiftingCursor.Dock = DockStyle.Fill;

            // WAYPOINT
            frmWayPoint = new FrmWayPoint();
            frmWayPoint.TopLevel = false;
            frmWayPoint.FormBorderStyle = FormBorderStyle.None;
            frmWayPoint.Dock = DockStyle.Fill;
            frmWayPoint.Handler += UpdateWayPoint;

            // WAYPOINT LIST
            frmWayPointList = new FrmWaypointList();
            frmWayPointList.TopLevel = false;
            frmWayPointList.FormBorderStyle = FormBorderStyle.None;
            frmWayPointList.Dock = DockStyle.Fill;
        }

        public void UpdateWayPoint(object sender, EventArgs e)
        {
            var eventType = (TransferDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });
            if (comp[0] == "EXIT")
            {
                // do something
            }
        }

        public void HandlerMainMenu(object sender, EventArgs e)
        {
            var eventType = (TransferDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            switch (comp[1].ToUpper())
            {
                case "PLOTTER":
                    viewMode = VIEWMODE.PLOTTER_SETUP;
                    ActiveForms(frmPlotterSetup);
                    break;

                case "SYS SETUP":
                    viewMode = VIEWMODE.SYS_SETUP;
                    ActiveForms(frmSysSetup);
                    break;

                case "WAYPOINTS":
                    viewMode = VIEWMODE.SELECT_WAYPOINT_LIST;
                    frmSelectWayPointList = new FrmSelectWayPointList();
                    frmSelectWayPointList.Handler += ShowWayPointList;
                    frmSelectWayPointList.Show();
                    break;
            }
        }

        public void ShowWayPointList(object sender, EventArgs e)
        {
            var eventType = (TransferDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });
            if (comp[0] == "LIST")
            {
                ActiveForms(frmWayPointList);
            }
        }

        public void HandlerAdjustDimmerAndConstrast(object sender, EventArgs e)
        {            
            pnlMain.Controls.Clear();
        }

        public void ClearForms()
        {
            pnlMain.Controls.Clear();
            viewMode = VIEWMODE.CLEAR;
        }

        public void ActiveForms(Form frm)
        {
            pnlMain.Controls.Clear();
            pnlMain.Controls.Add(frm);
            frm.Show();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            pnlMain.Controls.Clear();
            mode = TURNMODE.OFF;
            InitForms();
            XMLDataProvider.LoadDBInfo(Application.StartupPath + "\\Configs\\Config.xml");
        }

        private void btnPower_MouseDown(object sender, MouseEventArgs e)
        {
            if (mode == TURNMODE.OFF)
            {
                mode = TURNMODE.ON;
                lblStatus.Text = "Equipment is turned on";
            }
            else
            {
                numSecond = 0;
                viewMode = VIEWMODE.DIMMER_CONTRAST;
                ActiveForms(frmDimmerAndContrast);
                timeTurn.Start();
            }
        }

        private void btnPower_MouseUp(object sender, MouseEventArgs e)
        {
            if (numSecond < 3)
            {
                lblStatus.Text = "Equipment is turned on";
            }
            numSecond = 0;
            timeTurn.Stop();
        }

        private void timeTurn_Tick(object sender, EventArgs e)
        {
            numSecond++;
            if (numSecond >= 3)
            {
                mode = TURNMODE.OFF;
                ClearForms();
                lblStatus.Text = "Equipment is turned off";
            }
            else
            {
                lblStatus.Text = "Remains " + (3 - numSecond) + " second to equipment turn off";
            }
        }

        private void ProcessKeyEvent()
        {
            switch (viewMode)
            {
                case VIEWMODE.DIMMER_CONTRAST:
                    frmDimmerAndContrast.Focus();
                    frmDimmerAndContrast.ProcessKeyEvent(KeyEvent);
                    break;
                   
                case VIEWMODE.MAIN_MENU:
                    frmMainMenu.Focus();
                    frmMainMenu.ProcessKeyEvent(KeyEvent);
                    break;

                case VIEWMODE.PLOTTER_SETUP:
                    frmPlotterSetup.Focus();
                    frmPlotterSetup.ProcessKeyEvent(KeyEvent);
                    break;

                case VIEWMODE.SYS_SETUP:
                    frmSysSetup.Focus();
                    frmSysSetup.ProcessKeyEvent(KeyEvent);
                    break;

                case VIEWMODE.SHIFTING_CURSOR:
                    frmShiftingCursor.Focus();
                    frmShiftingCursor.ProcessKeyEvent(KeyEvent);
                    break;

                case VIEWMODE.WAYPOINT:
                    frmWayPoint.Focus();
                    frmWayPoint.ProcessKeyEvent(KeyEvent);
                    break;

                case VIEWMODE.SELECT_WAYPOINT_LIST:
                    frmSelectWayPointList.Focus();
                    frmSelectWayPointList.ProcessKeyEvent(KeyEvent);
                    break;
            }
        }

        private void btnMenu_Click(object sender, EventArgs e)
        {
            if (mode == TURNMODE.OFF)
            {
                return;
            }
            if (viewMode == VIEWMODE.SHIFTING_CURSOR)
            {
                frmShiftingCursor.ProcessKeyEvent(KEY.MENU);
            }
            else
            {
                viewMode = VIEWMODE.MAIN_MENU;
                ActiveForms(frmMainMenu);
            }
        }

        private void ptbUp_MouseDown(object sender, MouseEventArgs e)
        {
            this.ptbUp.BackgroundImage = global::simulate_control.Properties.Resources.up_icon_highlight;
        }

        private void ptbUp_MouseUp(object sender, MouseEventArgs e)
        {
            KeyEvent = KEY.UP;
            this.ptbUp.BackgroundImage = global::simulate_control.Properties.Resources.up_icon;
            ProcessKeyEvent();
        }

        private void ptbRight_MouseDown(object sender, MouseEventArgs e)
        {
            this.ptbRight.BackgroundImage = global::simulate_control.Properties.Resources.forward_icon_highlight;
        }

        private void ptbRight_MouseUp(object sender, MouseEventArgs e)
        {
            KeyEvent = KEY.RIGHT;
            this.ptbRight.BackgroundImage = global::simulate_control.Properties.Resources.forward_icon;
            ProcessKeyEvent();
        }

        private void ptbDown_MouseDown(object sender, MouseEventArgs e)
        {
            this.ptbDown.BackgroundImage = global::simulate_control.Properties.Resources.down_icon_highlight;
        }

        private void ptbDown_MouseUp(object sender, MouseEventArgs e)
        {
            KeyEvent = KEY.DOWN;
            this.ptbDown.BackgroundImage = global::simulate_control.Properties.Resources.down_icon;
            ProcessKeyEvent();
        }

        private void ptbLeft_MouseDown(object sender, MouseEventArgs e)
        {
            this.ptbLeft.BackgroundImage = global::simulate_control.Properties.Resources.back_icon_highlight;
        }

        private void ptbLeft_MouseUp(object sender, MouseEventArgs e)
        {
            KeyEvent = KEY.LEFT;
            this.ptbLeft.BackgroundImage = global::simulate_control.Properties.Resources.back_icon;
            ProcessKeyEvent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            KeyEvent = KEY.ENTER;
            ProcessKeyEvent();
        }

        private void btnDisp_Click(object sender, EventArgs e)
        {
            if (mode == TURNMODE.OFF)
            {
                return;
            }
            viewMode = VIEWMODE.SHIFTING_CURSOR;
            DispMode = Utils.GetNextDispMode(DispMode);
            switch (DispMode)
            {
                case DISPMODE.CURSOR_TURN_ON:
                    ActiveForms(frmShiftingCursor);
                    break;
            }
        }

        private void btnMarkMob_Click(object sender, EventArgs e)
        {
            if (mode == TURNMODE.OFF)
            {
                return;
            }
            viewMode = VIEWMODE.WAYPOINT;
            ActiveForms(frmWayPoint);
        }
    }
}
